﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace ArtemisTest
{

    public class InputSystem : CSystem
    {

        public void OnMouseButtonDown(object sender, MouseButtonEventArgs e)
        {
            var components = this.EntitySystem.Components.GetAllComponentsOfType(ComponentType.mouse_button_script);
            foreach (var c in components)
            {
                MouseButtonScript mbs = c.Value as MouseButtonScript;
                mbs.OnMouseButtonEvent(sender, e, true);
            }
            //Console.WriteLine("Button Down");
        }

        public void OnMouseButtonUp(object sender, MouseButtonEventArgs e)
        {
            var components = this.EntitySystem.Components.GetAllComponentsOfType(ComponentType.mouse_button_script);
            foreach (var c in components)
            {
                MouseButtonScript mbs = c.Value as MouseButtonScript;
                mbs.OnMouseButtonEvent(sender, e, false);
            }
            //Console.WriteLine("Button Up");
        }
        public void OnMouseMove(object sender, MouseMoveEventArgs e)
        {
            var components = this.EntitySystem.Components.GetAllComponentsOfType(ComponentType.mouse_move_script);
            foreach (var c in components)
            {
                MouseMoveScript mbs = c.Value as MouseMoveScript;
                mbs.OnMouseMoveEvent(sender, e);
            }

            //Console.WriteLine("Mouse Move");
        }
        public void OnKeyboardDown(object sender, KeyboardKeyEventArgs e)
        {
            var components = this.EntitySystem.Components.GetAllComponentsOfType(ComponentType.keyboard_key_script);
            foreach (var c in components)
            {
                KeyboardKeyScript kks = c.Value as KeyboardKeyScript;
                kks.OnKeyboardKeyEvent(sender, e,true);
            }
            //Console.WriteLine("Button Down");
        }
        public void OnKeyboardUp(object sender, KeyboardKeyEventArgs e)
        {
            var components = this.EntitySystem.Components.GetAllComponentsOfType(ComponentType.keyboard_key_script);
            foreach (var c in components)
            {
                KeyboardKeyScript kks = c.Value as KeyboardKeyScript;
                kks.OnKeyboardKeyEvent(sender, e, false);
            }
            //Console.WriteLine("Button Up");
        }

        /// <summary>
        /// OBSOLETE
        /// </summary>
        public override void Init()
        {
        }

        public void Init(GameWindow w)
        {
            w.Mouse.ButtonDown +=new EventHandler<MouseButtonEventArgs>(OnMouseButtonDown);
            w.Mouse.ButtonUp += new EventHandler<MouseButtonEventArgs>(OnMouseButtonUp); 
            w.Mouse.Move += new EventHandler<MouseMoveEventArgs>(OnMouseMove);
            w.Keyboard.KeyDown += new EventHandler<KeyboardKeyEventArgs>(OnKeyboardDown);
            w.Keyboard.KeyUp += new EventHandler<KeyboardKeyEventArgs>(OnKeyboardUp);
        }

        /// <summary>
        /// OBSOLETE
        /// </summary>
        /// <param name="dt"></param>
        public override void PreUpdate(double dt)
        {

        }

        public void PreUpdate(double dt, MouseDevice md, KeyboardDevice kd)
        {
            Dictionary<long, Component> mouseMoves = EntitySystem.Components.GetAllComponentsOfType(ComponentType.mouse_move_script);
            Dictionary<long, Component> mousePress = EntitySystem.Components.GetAllComponentsOfType(ComponentType.mouse_button_script);
            Dictionary<long, Component> keyboardPress = EntitySystem.Components.GetAllComponentsOfType(ComponentType.keyboard_key_script);
            Dictionary<long, Component> inputs = EntitySystem.Components.GetAllComponentsOfType(ComponentType.input);

            //Update Scripts
            foreach (var entry in mouseMoves) { ((MouseMoveScript)entry.Value).Update(dt); }
            foreach (var entry in mousePress) { ((MouseButtonScript)entry.Value).Update(dt); }
            foreach (var entry in keyboardPress) { ((KeyboardKeyScript)entry.Value).Update(dt); }
        }

        public override void Update(double dt)
        {
            throw new NotImplementedException();
        }

        public override void PostUpdate(double dt)
        {
            throw new NotImplementedException();
        }

        public InputSystem(EntitySystem es, ScriptSystem ss) : base(es,ss) { }
    }
}
